package serviceMonitor

import (
	"fmt"
)

func MonitorService(serviceName string) (err error) {
	defer func() { // 必须要先声明defer，否则不能捕获到panic异常
		if e := recover(); e != nil {
			//fmt.Printf("%s s7 protocol an error occurred:%v\n", time.Now().Format("2006-01-02 15:04:05.000"), e)
			err = fmt.Errorf("monitorService an error occurred:%v\n", e)
		}
	}()

	linuxSer := &LinuxService{serviceName}
	active, er := linuxSer.IsActive()
	if er != nil {
		return er
	}
	if !active {
		err = linuxSer.Start()
	}
	return
}
func StartService(serviceName string) (err error) {
	defer func() { // 必须要先声明defer，否则不能捕获到panic异常
		if e := recover(); e != nil {
			//fmt.Printf("%s s7 protocol an error occurred:%v\n", time.Now().Format("2006-01-02 15:04:05.000"), e)
			err = fmt.Errorf("monitorService an error occurred:%v\n", e)
		}
	}()
	linuxSer := &LinuxService{serviceName}
	active, er := linuxSer.IsActive()
	if er != nil {
		return er
	}
	if !active {
		err = linuxSer.Start()
	}
	return
}
func StopService(serviceName string) (err error) {
	defer func() { // 必须要先声明defer，否则不能捕获到panic异常
		if e := recover(); e != nil {
			//fmt.Printf("%s s7 protocol an error occurred:%v\n", time.Now().Format("2006-01-02 15:04:05.000"), e)
			err = fmt.Errorf("monitorService an error occurred:%v\n", e)
		}
	}()

	linuxSer := &LinuxService{serviceName}
	active, er := linuxSer.IsActive()
	if er != nil {
		return er
	}
	if active {
		err = linuxSer.Stop()
	}
	return
}
